<template>
  <div>
    <div class="searchWord">

      <el-form :inline="true" class="demo-form-inline">
        </el-form-item>
        <el-form-item label="模糊条件">
          <el-input style="width: 400px;" placeholder="请输入品牌名称或名称缩写查询" v-model="listGoodsBrandParam.keyword" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round="true" @click="loadbrand(1)">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" round="true" @click="openAdd ">添加</el-button>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="ids.length <= 0" type="danger" @click="delShop">删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" round="true" @click="openUpdate">修改</el-button>
        </el-form-item>
      </el-form>
    </div>


    <div>
      <el-table :data="brand" @selection-change="getChecks">
        <el-table-column type="selection" label="序号">
        </el-table-column>
        <el-table-column type="index" label="序号">
        </el-table-column>
        <el-table-column prop="goodsBrandName" label="品牌名">
        </el-table-column>
        <el-table-column prop="goodsBrandSimplename" label="品牌首字母">
        </el-table-column>
        <el-table-column label="显示">
          <template slot-scope="scope">
            {{ scope.row.goodsBrandStatus == 1 ? '否' : '是' }}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination :page-sizes="sizes" @size-change="sizeChangeFind" :current-page="listGoodsBrandParam.indexPage"
                     @current-change="changePage" background layout="sizes, prev, pager, next" :total="total"
                     :page-size="listGoodsBrandParam.pageSize">
      </el-pagination>
    </div>

    <el-dialog @close="clearAddForm" title="新增品牌" :visible.sync="addMovieVisible">
      <div>
        <el-form>
          <el-form-item label="品牌名称">
            <el-input v-model="addGoodsBrandParam.goodsBrandName"></el-input>
          </el-form-item>
          <el-form-item label="品牌首字母">
            <el-input v-model="addGoodsBrandParam.goodsBrandSimplename"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <template>
              <el-radio v-model="addGoodsBrandParam.goodsBrandStatus" label="0">是</el-radio>
              <el-radio v-model="addGoodsBrandParam.goodsBrandStatus" label="1">否</el-radio>
            </template>
          </el-form-item>
          <el-transfer v-model="addGoodsBrandParam.goodsGeneralIds" :data="AllGeneral" :titles="['待选商品大类','适用商品大类']"
                       :button-texts="['', '']">
          </el-transfer>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveShop">提 交</el-button>
      </div>
    </el-dialog>

    <el-dialog @close="clearUpForm" title="修改分类" :visible.sync="UpdateVisible">
      <div>
        <el-form>
          <el-form-item label="品牌名称">
            <el-input v-model="updateGoodsBrandParam.goodsBrandName"></el-input>
          </el-form-item>
          <el-form-item label="品牌首字母">
            <el-input v-model="updateGoodsBrandParam.goodsBrandSimplename"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-radio-group v-model="updateGoodsBrandParam.goodsBrandStatus">
              <el-radio :label="0">启用</el-radio>
              <el-radio :label="1">停用</el-radio>
            </el-radio-group>

          </el-form-item>
          <el-transfer v-model="updateGoodsBrandParam.goodsGeneralIds"
                       :right-default-checked="updateGoodsBrandParam.goodsGeneralIds" :data="AllGenerals"
                       :titles="['待选商品大类','适用商品大类']" :button-texts="['', '']">
          </el-transfer>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Update">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      listGoodsBrandParam: {
        indexPage: 1,
        pageSize: 3,
        keyword: null,

      },
      total: 0,
      sizes: [1, 2, 3, 5, 10, 20],
      brand: [],
      ids: [],
      addMovieVisible: false,
      UpdateVisible: false,
      addGoodsBrandParam: {
        goodsBrandName: null,
        goodsBrandSimplename: null,
        goodsBrandStatus: null,
        goodsGeneralIds: []
      },
      AllGeneral: [],
      AllGenerals: [],
      updateGoodsBrandParam: []
    }
  },

  methods: {
    loadGeneral() {
      this.$http.get("/goods-general/listGeneral").then(resp => {
        if (resp.data.code == 200) {
          for (var i = 0; i < resp.data.data.length; i++) {

            this.AllGeneral.push({
              key: resp.data.data[i].goodsGeneralId,
              label: resp.data.data[i].goodsGeneralName
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: '查询失败 ' + resp.data.msg
          });
        }

      })
    },
    loadbrand(page) {
      if (page != undefined) {
        this.listGoodsBrandParam.indexPage = page;
      }
      this.$http.post("/goods-brand/pageBrandInfo", this.listGoodsBrandParam).then(resp => {
        this.brand = resp.data.data.goodsBrandDtos;
        this.total = resp.data.data.total;
        this.listGoodsBrandParam.indexPage = resp.data.data.indexPage;
      })
    },
    getChecks(sel) {
      this.ids = [];
      for (let i = 0; i < sel.length; i++) {
        this.ids.push(sel[i].goodsBrandId);
      }
    },
    sizeChangeFind(size) {
      this.listGoodsBrandParam.pageSize = size;
      this.loadbrand();
    },
    changePage(page) {
      this.listGoodsBrandParam.indexPage = page;
      this.loadbrand();
    },
    delShop() {
      if (this.ids.length > 0) {
        this.$http.post("/goods-brand/removeBrandInfo", this.ids).then(resp => {
          if (resp.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.loadbrand();
          } else {
            this.$message.error('删除失败');
          }
        })
      }
    },
    saveShop() {
      this.$http.post("/goods-brand/addBrandInfo", this.addGoodsBrandParam).then(resp => {
        if (resp.data.code == 200) {
          this.addMovieVisible = !this.addMovieVisible;
          this.$message({
            message: '添加成功',
            type: 'success',

          });
          this.loadGeneral();
          this.clearAddForm();

        } else {
          this.$message.error('添加失败');
        }
      })
    },
    openAdd() {
      this.addMovieVisible = !this.addMovieVisible;
      this.loadGeneral();
    },

    clearAddForm() {
      this.addGoodsBrandParam = {
        goodsBrandName: null,
        goodsBrandSimplename: null,
        goodsBrandStatus: null,
        goodsGeneralIds: []
      }
    },
    clearUpForm() {
      this.updateGoodsBrandParam=[]
    },
    Update() {
      this.$http.post("/goods-brand/updateBrandInfo", this.updateGoodsBrandParam).then(resp => {
        if (resp.data.code == 200) {
          this.$message({
            message: '更新成功',
            type: 'success'
          });
          this.clearUpForm();
          this.loadbrand();
        } else {
          this.$message.error('更新失败');
        }

        this.UpdateVisible = !this.UpdateVisible;

      })

    },
    openUpdate() {
      if (!this.UpdateVisible) {
        if (this.ids.length <= 0 || this.ids.length > 1) {
          alert("选择错误")
          return;
        }
      }
      this.UpdateVisible = !this.UpdateVisible;
      if (this.UpdateVisible) {
        this.clearUpForm();
        this.AllGenerals = [];
        this.updateGoodsBrandParam.goodsGeneralIds = [];
        this.updateDate();
      }
    },
    updateDate(){
      this.$http.get("/goods-general/listGeneral").then(resp => {

        if (resp.data.code == 200) {

          for (var i = 0; i < resp.data.data.length; i++) {
            this.AllGenerals.push({
              key: resp.data.data[i].goodsGeneralId,
              label: resp.data.data[i].goodsGeneralName
            })
          }


        } else {
          this.$message({
            type: 'error',
            message: '查询失败 ' + resp.data.msg
          });
        }
      })

      this.$http.get("/goods-brand/getBrandById/" + this.ids[0]).then(resp => {
        if (resp.data.code == 200) {
          this.updateGoodsBrandParam = resp.data.data;
          this.updateGoodsBrandParam.goodsGeneralIds = resp.data.data.goodsGeneralIds;
        } else {
          this.$message({
            type: 'error',
            message: '查询失败 ' + resp.data.msg
          });
        }

      })

    }
  },
  created() {
    this.loadbrand();
  }
}
</script>

<style>
</style>
